package demo2;

public class CloneTest {
    public static void main(String[] args) throws CloneNotSupportedException{
        Animal dog = new Animal("大黄",18);
        Animal tmp = (Animal)dog.clone();
        tmp.money = (Money)dog.money.clone();
        Animal dogOne = tmp;

        System.out.println(dog);
        System.out.println(dogOne);
        System.out.println("=======================");
        dog.money.m = 99;
        System.out.println(dog);
        System.out.println(dogOne);
    }
}
//演示深浅拷贝
class Money implements Cloneable{
    public int m = 19;
    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}
//创建一个待拷贝的类
class Animal implements Cloneable{
    public String name;
    int age;
    public Animal(String name, int age) {
        this.name = name;
        this.age = age;
    }
    Money money = new Money();
    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }

    @Override
    public String toString() {
        return "Animal{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", money=" + money.m +
                '}';
    }
}
//困死了 图书馆好闷啊 进来就头晕

